package com.chuanyang.driverh.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Base64;

import com.chuanyang.driverh.BaseApplication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * SharedPreferences工具类
 * 使用键值对的形式存储数据
 * name--boolean
 * name--String ...
 * @author 说明：SharedPreferences的操作工具类，需要缓存到SharedPreferences中的数据在此设置。
 */
public class SharedPrefUtil {

	/**创建或打开名为"dkm"的xml文件*/
	private static final SharedPreferences prefs = BaseApplication.getInstance().getSharedPreferences("cyt", Context.MODE_PRIVATE);

	/** 是否是第一次使用应用(判断是够展现引导页) */
	public static void setFirstUse(boolean isFirstUse) {
		prefs.edit().putBoolean("isFirstUse",isFirstUse).commit();
	}
	public static boolean getFirstUse() {
		return prefs.getBoolean("isFirstUse",true);
	}

	/**设置登录状态(显示免登录,) */
	public static void setLogined(boolean isLogined) {
		prefs.edit().putBoolean("is_logined", isLogined).commit();
	}
	public static boolean getIsLogined() {
		return prefs.getBoolean("is_logined", false);
	}


	/** 是否是第一次使用应用(判断是够展现引导页) */
	public static void setInstalGps(boolean instalGps) {
		prefs.edit().putBoolean("instalGps",instalGps).commit();
	}
	public static boolean getInstalGps() {
		return prefs.getBoolean("instalGps",false);
	}



	/**设置token) */
	public static void setToken(String token) {
		prefs.edit().putString("token", token).commit();
	}
	public static String getToken() {
		return prefs.getString("token","");
	}


	/**设置userId) */
	public static void setUserId(String userId) {
		prefs.edit().putString("userId", userId).commit();
	}
	public static String getUserId() {
		return prefs.getString("userId","");
	}

	/**设置CarId) */
	public static void setCarId(int carId) {
		prefs.edit().putInt("carId", carId).commit();
	}
	public static int getCarId() {
		return prefs.getInt("carId",0);
	}


	/**设置userId) */
	public static void setFleetId(String fleetId) {
		prefs.edit().putString("fleetId", fleetId).commit();
	}
	public static String getFleetId() {
		return prefs.getString("fleetId","");
	}

	/**设置userName) */
	public static void setUserName(String username) {
		prefs.edit().putString("username", username).commit();
	}
	public static String getUserName() {
		return prefs.getString("username","");
	}



	/**设置phone) */
	public static void setPhone(String phone) {
		prefs.edit().putString("phone", phone).commit();
	}
	public static String getPhone() {
		return prefs.getString("phone","");
	}

	/**设置密码) */
	public static void setPwd(String password) {
		prefs.edit().putString("password", password).commit();
	}
	public static String getPwd() {
		return prefs.getString("password","");
	}

	/**设置当前定位城市) */
	public static void setCity(String city) {
		prefs.edit().putString("city", city).commit();
	}
	public static String getCity() {
		return prefs.getString("city","");
	}


	/**设置目标地址经纬度) */
	public static void setTargetLat(String targetLat) {
		prefs.edit().putString("targetLat", targetLat).commit();
	}
	public static String getTargetLat() {
		return prefs.getString("targetLat","");
	}

	public static void setTargetLog(String targetLog) {
		prefs.edit().putString("targetLog", targetLog).commit();
	}
	public static String getTargetLog() {
		return prefs.getString("targetLog","");
	}




	public static void setLongitude(String longitude) {
		prefs.edit().putString("longitude", longitude).commit();
	}
	public static String getLongitude(){
		return prefs.getString("longitude","");
	}

	public static void setLatitude(String latitude) {
		prefs.edit().putString("latitude", latitude).commit();
	}
	public static String getLatitude(){
		return prefs.getString("latitude","");
	}





	/**
	 * 是否躲避拥堵
	 *
	 * @return
	 */
	public static boolean isCongestion() {
		return prefs.getBoolean("congestion", true);
	}

	public static void setisCongestion(boolean isCongestion) {
		prefs.edit().putBoolean("congestion", isCongestion).commit();
	}
	/**
	 * 是否高速优先
	 *
	 * @return
	 */
	public static boolean isHightspeed() {
		return prefs.getBoolean("highspeed", false);
	}

	public static void setisHightspeed(boolean isHightspeed) {
		prefs.edit().putBoolean("highspeed", isHightspeed).commit();
	}
	/**
	 * 不走高速

	 *
	 * @return
	 */
	public static boolean isAvoidhightspeed() {
		return prefs.getBoolean("isAvoidhightspeed", false);
	}

	public static void setisAvoidhightspeed(boolean isAvoidhightspeed) {
		prefs.edit().putBoolean("isAvoidhightspeed", isAvoidhightspeed).commit();
	}

	/**
	 * 避免收费
	 * @return
	 */
	public static boolean isCost() {
		return prefs.getBoolean("isCost", false);
	}

	public static void setisCost(boolean isCost) {
		prefs.edit().putBoolean("isCost", isCost).commit();
	}
	/**
	 * 是否直接导航
	 *
	 * @return
	 */
	public static boolean isDirect()
	{
		return prefs.getBoolean("isDirect", false);
	}

	public static void setisDirect(boolean isDirect) {
		prefs.edit().putBoolean("isDirect", isDirect).commit();
	}






	/**
	 * 将Object信息Base64后存入Preferences
	 *
	 * @param name
	 * @param obj
	 */

	public static <T> void setPreferences(String name, T obj) {
		if (obj == null) {
			prefs.edit().putString(name, null).commit();
			return;
		}
		ByteArrayOutputStream toByte=null;
		ObjectOutputStream oos=null;
		try {
			toByte = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(toByte);
			oos.writeObject(obj);
			// 对byte[]进行Base64编码
			String obj64 = new String(Base64.encode(toByte.toByteArray(),Base64.DEFAULT));
			prefs.edit().putString(name, obj64).commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (toByte!=null)toByte.close();
				if (oos!=null)oos.close();
			}catch (Exception e){

			}
		}
	}

	/**
	 * 获取SharePreference中值，Base64解码之后传出
	 *
	 * @param name
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getPreferences(String name, T obj) {
		ByteArrayInputStream bais = null;
		ObjectInputStream ois =null;
		try {
			String obj64 = prefs.getString(name, "");
			if (TextUtils.isEmpty(obj64)) {
				return null;
			}
			byte[] base64Bytes = Base64.decode(obj64, Base64.DEFAULT);
			bais = new ByteArrayInputStream(base64Bytes);
			ois = new ObjectInputStream(bais);
			return (T) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (bais!=null)bais.close();
				if (ois!=null)ois.close();
			}catch (Exception e){

			}
		}
		return null;
	}

	/**
	 * 删除指定的sp
	 */
	public static void clearSp(String  name)
	{
		prefs.edit().remove("name").commit();
	}
	/**
	 * 删除sp
	 */
	public static void clear()
	{
		prefs.edit().clear().commit();
	}


}
